package com.foin.city.utils;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Point;
import android.util.DisplayMetrics;
import android.view.View;

/**
 * @author Foin
 * @version 1.0
 * @data 2017/4/13
 * @desc 屏幕距离计算的一些工具方法
 */
public class DisplayUtil {
    /*
     * 获取屏幕尺寸与密度.
     *
     * @param context the context
     * @return mDisplayMetrics
     */
    public static DisplayMetrics getDisplayMetrics(Context context) {
        Resources resources;
        if (context == null) {
            resources = Resources.getSystem();
        } else {
            resources = context.getResources();
        }
        return resources.getDisplayMetrics();
    }

    /*
     * dp转px
     */
    public static float dp2px(float dpValue) {
        //屏幕的像素密度
        float DENSITY = Resources.getSystem().getDisplayMetrics().density;
        return dpValue * DENSITY + 0.5f;
    }

    /**
     * @desc sp转px
     */
    public static float sp2px(Context context, int sp) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (sp * fontScale + 0.5f);
    }

    /*
     * 获取除通知栏的高度
     *
     * @param activity
     */
    public static int getNotificationHeight() {
        return Resources.getSystem().
                getDimensionPixelSize(Resources.getSystem().
                        getIdentifier("status_bar_height", "dimen", "android"));
    }

    /**
     * @desc 测量View的高宽
     */
    public static Point getViewWh(View view) {
        //测量方法
        int width = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        int height = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
        view.measure(width, height);
        return new Point(view.getMeasuredWidth(), view.getMeasuredHeight());
    }
}
